package cn.iocoder.yudao.module.system.controller.admin.article;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.system.controller.admin.article.vo.SystemArticlePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.article.vo.SystemArticleRespVO;
import cn.iocoder.yudao.module.system.controller.admin.article.vo.SystemArticleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.article.SystemArticleDO;
import cn.iocoder.yudao.module.system.service.article.SystemArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 弹窗文章")
@RestController
@RequestMapping("/system/article")
@Validated
public class SystemArticleController {

    @Resource
    private SystemArticleService articleService;

    @PostMapping("/create")
    @Operation(summary = "创建弹窗文章")
    @PreAuthorize("@ss.hasPermission('system:article:create')")
    public CommonResult<Long> createArticle(@Valid @RequestBody SystemArticleSaveReqVO createReqVO) {
        return success(articleService.createArticle(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新弹窗文章")
    @PreAuthorize("@ss.hasPermission('system:article:update')")
    public CommonResult<Boolean> updateArticle(@Valid @RequestBody SystemArticleSaveReqVO updateReqVO) {
        articleService.updateArticle(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除弹窗文章")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:article:delete')")
    public CommonResult<Boolean> deleteArticle(@RequestParam("id") Long id) {
        articleService.deleteArticle(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得弹窗文章")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:article:query')")
    public CommonResult<SystemArticleRespVO> getArticle(@RequestParam("id") Long id) {
        SystemArticleDO article = articleService.getArticle(id);
        return success(BeanUtils.toBean(article, SystemArticleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得弹窗文章分页")
    @PreAuthorize("@ss.hasPermission('system:article:query')")
    public CommonResult<PageResult<SystemArticleRespVO>> getArticlePage(@Valid SystemArticlePageReqVO pageReqVO) {
        PageResult<SystemArticleDO> pageResult = articleService.getArticlePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SystemArticleRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出弹窗文章 Excel")
    @PreAuthorize("@ss.hasPermission('system:article:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportArticleExcel(@Valid SystemArticlePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SystemArticleDO> list = articleService.getArticlePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "弹窗文章.xls", "数据", SystemArticleRespVO.class,
                        BeanUtils.toBean(list, SystemArticleRespVO.class));
    }

}